﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using VitCode.BaseDataPageCreator.Setting;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;
using Menu = VitCode.Castle.Entities.Menu;


namespace VitCode.Web.BackStageManagement
{
    public partial class MenuInsert : VcPage
    {
        readonly BaseDataManager help = new BaseDataManager();
        private ObjXmlReader m_XmlReader;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LabelId.Text = publ.GetUrlToReceive(Request.QueryString["id"].Trim());
                string xmlPath = AppDomain.CurrentDomain.BaseDirectory + "BackStageManagement\\PermissionEnum.xml";
                m_XmlReader = new ObjXmlReader(xmlPath);
                XmlNode node = m_XmlReader.GetXmlNode("Permission/Operate");
                var nodeList = m_XmlReader.GetNodeList("Permission/Operate");
                if (nodeList == null || nodeList.Count <= 0) return;
                for (var i = 0; i < nodeList.Count; i++)
                {
                    string strText = m_XmlReader.GetNodeAttributeValue(nodeList[i], "Text", true);
                    string strValue = m_XmlReader.GetNodeAttributeValue(nodeList[i], "Value", true);
                    var item = new ListItem { Text = strText, Value = strValue };
                    CheckListOperate.Items.Add(item);
                }
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("MenuList.aspx", false);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var objMenu = new Menu();
            string strName = ZLTextBox_MenuName.Text.Trim();
            string strUrlPath = ZLTextBox_FileName.Text.Trim();
            string strMemo = ZLTextBox_Memo.Text.Trim();
            string strSorkKey = ZLTextBox_SortKey.Text.Trim();
            string strKeyWord = ZLTextBox_KeyWord.Text.Trim();
            string strParentId = LabelId.Text.Trim();
            string strOperateList = "";
            foreach (ListItem item in CheckListOperate.Items)
            {
                
                    strOperateList += item.Value.Trim() + ";";
               
            }
            if (!string.IsNullOrEmpty(strOperateList))
            {
                strOperateList = strOperateList.Substring(0, strOperateList.Length - 1);
            }
            objMenu.Operate = strOperateList;
            objMenu.Name = strName;
            objMenu.UrlPath = strUrlPath;
            objMenu.Memo = strMemo;
            objMenu.KeyWord = strKeyWord;
            objMenu.SortKey = strSorkKey;
            CastleAccess acc = new CastleAccess();
            if (strParentId != "0")
            {
                var lstObj = acc.GetObjectList(new Menu().GetType().Name, "Id", strParentId.Trim());
                if (lstObj != null && lstObj.Count > 0)
                {
                    var objParentMenu = (Menu)(lstObj[0]);
                    objMenu.Parent = objParentMenu;
                }
            }

            bool access = help.SaveOrUpdateObj(objMenu);
            if (access)
            {
                Alert.MessageBox("添加成功!");
            }
        }

    }
}
